home *** CD-ROM | disk | FTP | other *** search
- program CleanHex;
-
- { two real short functions for converting hex chars to integer and vice versa }
- { Scott Martin, The Forbin Project, Minneapolis, Mn. }
-
- const
- HexC : string [16] = '0123456789ABCDEF';
-
- type
- str04 = string [04];
-
- var
- x : integer;
- hexstring : str04;
-
- { convert upper case hex input string, 4 chars long, to a 2-byte integer }
- { edit the input before using HexCon! }
- function HexCon (instr : str04) : integer;
- begin
- HexCon := pred(pos(instr[1],HexC)) shl 12 +
- pred(pos(instr[2],HexC)) shl 8 +
- pred(pos(instr[3],HexC)) shl 4 +
- pred(pos(instr[4],HexC));
- end;
-
- { convert an integer to its 4-byte hex representation }
- function Hex4 (n : integer) : str04;
- begin
- Hex4 := copy (HexC,succ(hi (n) shr 4), 1) +
- copy (HexC,succ(hi (n) and 15),1) +
- copy (HexC,succ(lo (n) shr 4), 1) +
- copy (HexC,succ(lo (n) and 15),1);
- end;
-
- begin
- repeat
- write ('Enter an integer ');
- readln (x);
- hexstring := hex4(x);
- writeln ('Hex representation = ',hexstring,' Integer = ',HexCon (hexstring));
- until x = 0;
- end.